<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Float grid demo</title>

  <link rel="stylesheet" href="demo.css"/>
  <script src="../dist/gridstack-all.js"></script>

</head>
<body>
  <div class="container-fluid">
    <h1>Float grid demo</h1>
    <div>
      <a class="btn btn-primary" onClick="addNewWidget()" href="#">Add Widget</a>
      <a class="btn btn-primary" onClick="makeNewWidget()" href="#">Make Widget</a>
      <a class="btn btn-primary" onclick="toggleFloat()" id="float" href="#">float: true</a>
    </div>
    <br><br>
    <div class="grid-stack"></div>
  </div>
  <script src="events.js"></script>
  <script type="text/javascript">
    let grid = GridStack.init({
      float: true, 
      // disableResize: true, // TEST no resizing, but dragging
      resizable: { handles: 'all'} // do all sides for testing
    });
    addEvents(grid);

    let items = [
      {x: 1, y: 1}, //, locked:true, content:"locked"},
      {x: 2, y: 2, w: 3},
      {x: 4, y: 2},
      {x: 3, y: 1, h: 2},
      {x: 0, y: 6, w: 2, h: 2}
    ];
    let count = 0;

    getNode = function() {
      let n = items[count] || {
        x: Math.round(12 * Math.random()),
        y: Math.round(5 * Math.random()),
        w: Math.round(1 + 3 * Math.random()),
        h: Math.round(1 + 3 * Math.random())
      };
      n.content = n.content || String(count);
      count++;
      return n;
    };

    addNewWidget = function() {
      let w = grid.addWidget(getNode());
    };

    makeNewWidget = function() {
      let n = getNode();
      let doc = document.implementation.createHTMLDocument();
      doc.body.innerHTML = `<div class="item" gs-x="${n.x}" gs-y="${n.y}" gs-w="${n.w || 1}" gs-h="${n.h || 1}"><div class="grid-stack-item-content">${n.content}</div></div>`;
      let el = doc.body.children[0];
      grid.el.appendChild(el);
      // example showing when DOM is created elsewhere (eg Angular/Vue/React) and GS is used to convert to a widget
      let w = grid.makeWidget(el);
    };

    toggleFloat = function() {
      grid.float(! grid.getFloat());
      document.querySelector('#float').innerHTML = 'float: ' + grid.getFloat();
    };
    addNewWidget();
  </script>
</body>
</html>
